package com.sanyue.springboot.servlet;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 过滤器
 * 解读：
 *  1.@WebFilter表示该类是一个过滤器
 *  2.urlPatterns映射地址
 *  3.urlPatterns = {"/css/*", "/images/*"} 表示当请求css/目录资源或者 /images/目录下资源的时候会请求该过滤器
 *  4.当请求过滤器后，会经过拦截器，是否拦截需要根据拦截器的拦截规则
 *  5.注意拦截器拦截是俩个*，过滤器是一个* 例如"/css/*"
 * @author: Guan
 **/
// @WebFilter(urlPatterns = {"/css/*", "/images/*"})
@Slf4j
public class SbFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("--Filter init--");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        log.info("--doFilter--");
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        log.info("过滤器请求的uri为{}",request.getRequestURI());
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
        log.info("--Filter destroy--");
    }
}
